<?php


namespace app\api\controller;
use app\BaseController;
use app\common\controller\Api;
use app\Request;
use fast\Redis;
use fast\Exclfile;
use think\facade\Db;
class Ajax extends Api {
    public function ajaxupload(Request $request){
        $request = $this->request->request();
        unset($request['token']);
        $uid = $this->uid;
        $name = $request['action'];
        if ($uid == '' || $name == '' ) $this -> error('参数错误');
        $uplaodsrc = '/storage/'.$name;
        $upload = new Exclfile(SHUJUCUNCHU);
        switch ($name){
            case 'health':
                $url = $upload -> ImgSaceUpload('health',$uplaodsrc."/".$uid);
                break;
            case "myavater":
                $url = $upload -> ImgSaceUpload('myavater',$uplaodsrc);
                break;
        }
        if($url){
            $this -> success('上传成功',['url'=>$url]);
//            return apiBack('success', '上传成功', '10000',['url'=>$url]);
        }else{
            $this -> error("上传失败");
        }
    }

    public function healthupload(Request $request){
        $uid = $request->get('uid');
        $healthid = $request->get('id');


        if ($uid == "" || $healthid == "") $this -> error('参数错误');
//        $file=$_FILES['uploadfile_ant'];
//        $dir='upload/weixin/'.date("Ymd").'/';
//        $filename=$dir.md5(microtime(true)).'.'.explode('.',$file['name'])[count(explode('.',$file['name']))-1];
//        if(!file_exists($_SERVER['DOCUMENT_ROOT'].$dir)){
//            mkdir($_SERVER['DOCUMENT_ROOT'].$dir,0777,true);
//        }
        var_dump($uid);die;
        $file=$_FILES['uploadfile_ant'];
        $dir='upload/weixin/'.date("Ymd").'/';
        $filename=$dir.md5(microtime(true)).'.'.explode('.',$file['name'])[count(explode('.',$file['name']))-1];
        if(!file_exists($_SERVER['DOCUMENT_ROOT'].$dir)){
            mkdir($_SERVER['DOCUMENT_ROOT'].$dir,0777,true);
        }

        if(move_uploaded_file($file['tmp_name'],$_SERVER['DOCUMENT_ROOT'].$filename)){
            $info=Db::name("infomation")->where(['id'=>$healthid])->value('health');
            if($info==""||$info==null){
                $data['health']=$filename;
            }else{
                $data['health']=$info.$filename;
            }
            Db::startTrans();
            $res=Db::name("health_file")->where(['id'=>$healthid])->update($data);
            if($res!==false){
                Db::commit();
                return json(true);
            }else{
                Db::rollback();
                return json(false);
            }
        }else{
            return json(false);
        }
    }
}
